package com.cloud.common.magic_init.handler;

import com.cloud.common.constants.DataTableFields;
import com.cloud.utils.DateTimeUtils;
import com.cloud.utils.SQLFilterUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.core.config.MagicFunction;
import org.ssssssss.script.annotation.Comment;
import org.ssssssss.script.annotation.Function;

import java.util.Calendar;

/**
 * 自定义扩展函数
 *
 * @author yzj
 */
@Component
public class MagicCustomFunction implements MagicFunction {

    @Function
    @Comment("获取当前系统年份")
    public static Integer yearNum() {
        Calendar date = Calendar.getInstance();
        return date.get(Calendar.YEAR);
    }

    @Function
    @Comment("获取当前系统月份")
    public static Integer monthNum() {
        Calendar date = Calendar.getInstance();
        return date.get(Calendar.MONTH) + 1;
    }

    @Function
    @Comment("日期格式化")
    public static String dateFormat(@Comment("目标日期") String date, @Comment("格式化模式") String format) {
        return DateTimeUtils.setDateFormat(date, format);
    }

    @Function
    @Comment("判断对象值是否为空")
    public static Boolean ifNull(@Comment("目标值") Object object) {
        return ObjectUtils.isNotEmpty(object);
    }

    @Function
    @Comment("判断字符值是否为空")
    public static Boolean ifNull(@Comment("目标值") String value) {
        return StringUtils.isNotEmpty(value);
    }

    @Function
    @Comment("自定义数据权限SQL")
    public static String sqlFilter(@Comment("部门别称") String deptAlias, @Comment("用户别称") String userAlias) {
        return SQLFilterUtils.sqlFilter(deptAlias, DataTableFields.Auth_Dept_Id, userAlias, DataTableFields.Auth_User_Id);
    }

}
